import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../../base/utils/logger.dart';
import '../../../../data/api.dart';
import '../../../../data/models/doctor_list_res.dart';
import '../../../../data/models/get_article_res.dart';

class HomePageController extends GetxController {
  var doctorList = [].obs;
  var articleList = [].obs;

  void onInit() {
    super.onInit();
    getArticleList();
    getDoctorList();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  getArticleList() async {
    try {
      EasyLoading.show(status: 'loading...');
      List<ArticleModel>? _list = await Api.getArticleList(
        page: 1,
        pageSize: 2,
      );
      if (_list != null) {
        articleList.value = [..._list];
      }
    } catch (e) {
      loggy.error(e.toString());
    } finally {
      EasyLoading.dismiss();
    }
  }

  getDoctorList() async {
    try {
      EasyLoading.show(status: 'loading...');
      List<DoctorModel>? _list = await Api.getDoctorList(
        page: 1,
        pageSize: 2,
      );
      if (_list != null) {
        doctorList.value = [..._list];
      }
    } catch (e) {
      loggy.error(e.toString());
    } finally {
      EasyLoading.dismiss();
    }
  }
}
